package com.xiaochong.bitong.job.schedule;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.job.config.ExecutorConfig;
import com.xiaochong.bitong.job.service.CoinPriceCountService;
import com.xiaochong.tir.common.data.po.ExchangeInfo;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @Description: 计算每个货币平均价格
 * @Author: org
 * @CreateDate: 2018/8/16 14:17
 */
@Component
public class CoinPriceCountScheduled {

    @Autowired
    private CoinPriceCountService coinPriceCountService;


    java.util.concurrent.Semaphore semaphore = new Semaphore(1, true);
    /**
     *货币平均价格计算
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0/10 * * * * ?")
    public void calculateCoinPrice() throws Exception {
        LogTrace.beginTrace();
        try {
            if(semaphore.tryAcquire(1)){
                long start = System.currentTimeMillis();
                LogTrace.info("thread计算货币平均价格计算","start",String.valueOf(start));
                //CountDownLatch countDownLatch = new CountDownLatch(1);
                coinPriceCountService.calculateCoinPrice();
                LogTrace.info("thread计算货币平均价格计算","end",String.valueOf(System.currentTimeMillis()-start));
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error(e.getMessage() ,e);
        }finally {
            semaphore.release();
        }

       // countDownLatch.countDown();
       // countDownLatch.await();
        //
    }
    /**
     *货币货币市值排行计算
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0/20 * * * * ?")
    public void calculateCoinMarketCapRank() throws Exception {
        LogTrace.beginTrace();
        try {
            if(semaphore.tryAcquire(1)){
                long start = System.currentTimeMillis();
                LogTrace.info("thread货币货币市值排行计算","start",String.valueOf(start));
                coinPriceCountService.calculateCoinMarketCapRank();
                LogTrace.info("thread货币货币市值排行计算","end",String.valueOf(System.currentTimeMillis()-start));
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error(e.getMessage() ,e);
        }finally {
            semaphore.release();
        }

    }
}
